<template>
	<view>
		<view class="search">
			<my-search @click="toSearch">搜索</my-search>
		</view>
		<!-- 轮播图 -->
		<swiper circular indicator-dots autoplay :interval="3000" :duration="1000">
			<swiper-item v-for="i in sliderList" :key="i.goods_id">
				<navigator :url="`/subpkg/goods_detail/goods_detail?goods_id=${i.goods_id}`">
					<image :src="i.image_src" />
				</navigator>
			</swiper-item>
		</swiper>
		<!-- 导航区 -->
		<view class="nav">
			<view class="nav-item" v-for="(item, index) in navList" :key="index" @click="handlerNav(item)">
				<image :src="item.image_src" />
			</view>
		</view>
		<!-- 楼层区域 -->
		<view class="floor">
			<view class="floor-item" v-for="(item, index) in floorList" :key="index">
				<image :src="item.floor_title.image_src" mode="widthFix" />
				<view class="product">
					<view class="left-img">
						<image @click="gotoList(item.product_list[0])" :style="{width: item.product_list[0].image_width + 'rpx'}" :src="item.product_list[0].image_src" mode="widthFix"/>
					</view>
					<view class="right-img">
						<image @click="gotoList(v)" v-for="(v,i) in item.product_list" :key="i" v-if="i != 0" :src="v.image_src" mode="widthFix" :style="{width: v.image_width + 'rpx'}"/>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapGetters } from 'vuex'
	import tabbarBadge from '../../mixins/tabbar-badge.js'
	export default {
		name: 'Home',
		mixins: [ tabbarBadge ],
		data() {
			return {
				sliderList: [],
				navList: [],
				floorList: []
			};
		},
		onLoad() {
			this.getSlider()
			this.getNavList()
			this.getFloorList()
		},
		methods: {
			// 获取轮播图请求方法
			async getSlider() {
				const { data: res } = await uni.$http.get('home/swiperdata')
				if (res.meta.status !== 200) uni.$showToast()
				this.sliderList = res.message
			},
			// 获取导航栏数据请求方法
			async getNavList() {
				const { data: res } = await uni.$http.get('home/catitems')
				if (res.meta.status !== 200) uni.$showToast()
				this.navList = res.message
			},
			// 获取楼层数据的方法
			async getFloorList() {
				const { data: res } = await uni.$http.get('home/floordata')
				if (res.meta.status !== 200) uni.$showToast()
				this.floorList = res.message
			},
			// 点击跳转方法
			handlerNav(item) {
				if (item.name === '分类') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},
			// 楼层图片跳转方法
			gotoList(item) {
				uni.navigateTo({
					url: `/subpkg/goods_list/goods_list?${item.navigator_url.split('?')[1]}`
				})
			},
			// 跳转至搜索页
			toSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
.search {
	position: sticky;
	top: 0;
	z-index: 999;
}
swiper {
	image {
		width: 100%;
		height: 300rpx;
	}
}
.nav {
	display: flex;
	justify-content: space-around;
	margin-top: 10rpx;
	image {
		width: 128rpx;
		height: 140rpx;
	}
}
.floor {
	.floor-item {
		padding: 10rpx;
		image {
			width: 100%;
		}
		.product {
			display: flex;
			.right-img {
				display: flex;
				flex-wrap: wrap;
				justify-content: space-evenly;
				image {
					height: 93px !important;
				}
			}
		}
	}
}
</style>
